package com.cty.dao.impl;

import com.cty.dao.HeroDao;
import com.cty.vo.Hero;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Auther: cty
 * @Date: 2020/2/14 21:23
 * @Description:
 * @version: 1.0
 */
public class HeroDaoImpl implements HeroDao {
    @Override
    public Hero getHeroInfo(String name) {
        //声明存储对象
        Hero u=null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接对象
            Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mytestdb","root","123456");
            //创建sql命令
            String sql="select * from hero where name=?";
            //创建sql命令对象
            PreparedStatement ps=conn.prepareStatement(sql);
            //给占位符赋值
            ps.setString(1,name);
            //遍历查询
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                u=new Hero();
                u.setId(rs.getInt("id"));
                u.setName(rs.getString("name"));
                u.setPrice(rs.getString("price"));
                u.setRole(rs.getString("role"));
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return u;
    }
}
